var e = require("@babel/runtime/helpers/typeof.js"), o = require("FFE95507875DE5CF998F3D00DB5D6824.js"), n = (require("26C7E8E6875DE5CF40A180E17E6D6824.js"), 
o.apiDomain), a = o.appName;
const app = getApp()

module.exports = {
    chooseImage: function(e) {
        console.log("ai.chooseImage", e);
        var o = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "camera", "album" ],
            success: function(n) {
                var a = n.tempFilePaths[0];
                console.log("choolseImage tmpFilePath:", a), o.saveAndJump(a, e);
            },
            fail: function(e) {
                console.log("取消了选择照片");
            }
        });
    },
    chooseImageByCamera: function(e) {
        console.log("choosebycamera", e);
        var o = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "camera" ],
            success: function(n) {
                console.log("choose by camera success:"), console.log(n);
                var a = n.tempFilePaths[0];
                o.saveAndJump(a, e);
            },
            fail: function(e) {
                console.log("取消了");
            }
        });
    },
    chooseImageByAlbum: function(e) {
        console.log("chooseFromAlbum", e);
        var o = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "album" ],
            success: function(n) {
                var a = n.tempFilePaths[0];
                o.saveAndJump(a, e);
            }
        });
    },
    saveAndJump: function(e, o) {
        console.log("ai.saveAndJump", o), wx.navigateTo({
            url: "/pages/detail/detail?type=" + o + "&filePath=" + e
        });
    },
    recognize: function(e, o) {
        console.log("ai.recognize", e, o);
        var i = this;
        wx.showLoading({
            title: "图像分析中"
        });
        var t = wx.getStorageSync("openid");
        return new Promise(function(s, c) {
            return wx.uploadFile({
                url: n + "/ai/recognize",
                filePath: e,
                name: "file",
                formData: {
                    type: o,
                    openid: t,
                    appname: a
                },
                success: function(e) {
                    if (console.log("识别结果:", e), !i.isJSON(e.data)) return wx.showToast({
                        title: "识别失败,请稍后重试"
                    }), {};
                    var o = JSON.parse(e.data);
                    var skin = app.globalData.skinscore;
                    console.log("ai.recd:", o), 200 == o.code ? s({
                        imgUrl: o.result.imgUrl,
                        aiResult: o.result.aiResult,
                        skin:o.result.aiResult[0].score
                    }) : c(o);
                    app.globalData.skinscore = o.result.aiResult[0].score
                    console.log('666',app.globalData.skinscore)
                },
                fail: err=> {
                  console.log("fail",err)
                    return wx.showToast({
                        title: "识别失败，请稍后重试"
                    }), {};
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        });
    },
    recognizeWord: function(o) {
        console.log("ai.recognizeWord", o), wx.showLoading({
            title: "图像分析中"
        });
        var i = wx.getStorageSync("openid");
        return new Promise(function(t, s) {
            return wx.uploadFile({
                url: n + "/ai/recognize",
                filePath: o,
                name: "file",
                formData: {
                    type: "word",
                    openid: i,
                    appname: a
                },
                success: function(o) {
                    console.log("ai.res", o), console.log("res type:");
                    var n = e(o.data);
                    if (console.log(n), "string" == n) {
                        var a = JSON.parse(o.data);
                        if (console.log("resdataObj below"), console.log(a), a instanceof Array) console.log("数组"), 
                        t({
                            code: 200,
                            msg: "success",
                            result: a
                        }); else a instanceof Object && (console.log("json对象"), t(a));
                    } else s("");
                },
                fail: function() {
                    wx.showToast({
                        title: "识别失败"
                    }), s("");
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        });
    },
    recognizeFace: function(e) {
        console.log("ai.recognizeFace"), wx.showLoading({
            title: "图像分析中"
        });
        var o = wx.getStorageSync("openid");
        return new Promise(function(i, t) {
            return wx.uploadFile({
                url: n + "/ai/recognize",
                filePath: e,
                name: "file",
                formData: {
                    type: "face",
                    openid: o,
                    appname: a
                },
                success: function(e) {
                    var o = JSON.parse(e.data);
                    console.log("res2:"), console.log(o), i({
                        res: o
                    });
                },
                fail: function() {
                    return wx.showToast({
                        title: "识别失败"
                    }), {};
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        });
    },
    recognizeCommon: function(e, o) {
        var i = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "图像分析中", t = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : "出错了~";
        console.log("ai.recognizeCommon"), wx.showLoading({
            title: i
        });
        var s = wx.getStorageSync("openid");
        return new Promise(function(i, c) {
            return wx.uploadFile({
                url: n + "/ai/recognize",
                filePath: e,
                name: "file",
                formData: {
                    type: o,
                    openid: s,
                    appname: a
                },
                success: function(e) {
                    var o = JSON.parse(e.data);
                    i({
                        res: o
                    });
                },
                fail: function() {
                    return wx.showToast({
                        title: t
                    }), {};
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        });
    },
    isJSON: function(o) {
        if ("string" == typeof o) try {
            var n = JSON.parse(o);
            return !("object" != e(n) || !n);
        } catch (e) {
            return console.log("error：" + o + "!!!" + e), !1;
        }
        return !1;
    }
};